package com.example.buyticketlishi.view.ac;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.baselibrary.Base.BaseActivity;
import com.example.baselibrary.Bean.LiShiPointListResultBean;
import com.example.baselibrary.Bean.LiShiaddOrRelieveRequsetBean;
import com.example.baselibrary.Bean.LiShifindLocationResultBean;
import com.example.baselibrary.Bean.LiShifindTerminalNameResultBean;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.baselibrary.Utils.ToastUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.RetrufitHelp.BaseSubscriber;
import com.example.buyticketlishi.RetrufitHelp.ExceptionHandle;
import com.example.buyticketlishi.RetrufitHelp.RetrofitClient;
import com.example.buyticketlishi.bean.JJHPointListResultBean;
import com.example.buyticketlishi.bean.JJHaddOrRelieveRequsetBean;
import com.example.buyticketlishi.bean.JJHaddOrRelieveResultBean;
import com.example.buyticketlishi.bean.JJHfindPointRequestBean;
import com.example.buyticketlishi.bean.JJHfindPointResultBean;
import com.example.buyticketlishi.bean.JJHfindTerminalNameRequestBean;
import com.example.buyticketlishi.bean.JJHfindTerminalNameResultBean;
import com.example.buyticketlishi.wightview.InputInformation;

import java.util.ArrayList;
import java.util.List;

public class LiShiSettingActivity extends BaseActivity {

    private EditText ipInput;
    private InputInformation pointName;
    private InputInformation locationName;
    private InputInformation meationName;
    private Button btBind;
    private Button btUnBind;
    private Button btQuit;
    private Button btBack;

    private OptionsPickerView pvNoLinkOptions, pvNoLinkOptionsecond, pvNoLinkOptionThread;

    private List<LiShiPointListResultBean.DataBean> pointListData;
    private List<String> pointListString;
    private Long scenicSpotBusinessOrgId;
    private String scenicSpotBusinessOrgName;


    private JJHfindPointRequestBean findLocationBean;
    private List<LiShifindLocationResultBean.DataBean> locationListData;
    private List<String> LocationListString;
    private Long locationId;
    private String locationNa;

    private JJHfindTerminalNameRequestBean meationBean;
    private List<LiShifindTerminalNameResultBean.DataBean> meationList;
    private List<String> meationListString;
    private Long terminalId;
    private String terminalName;

    private JJHaddOrRelieveRequsetBean addBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getView() {
        return R.layout.activity_li_shi_setting;
    }

    @Override
    public void init() {

        if (addBean == null) {
            addBean = new JJHaddOrRelieveRequsetBean();
        }

        if (pointListData == null) {
            pointListData = new ArrayList<>();
        }

        if (pointListString == null) {
            pointListString = new ArrayList<>();
        }

        if (locationListData == null) {
            locationListData = new ArrayList<>();
        }
        if (LocationListString == null) {
            LocationListString = new ArrayList<>();
        }

        if (meationList == null) {
            meationList = new ArrayList<>();
        }
        if (meationListString == null) {
            meationListString = new ArrayList<>();
        }

        if (findLocationBean == null) {
            findLocationBean = new JJHfindPointRequestBean();
        }

        if (meationBean == null) {
            meationBean = new JJHfindTerminalNameRequestBean();
        }


        ipInput = findViewById(R.id.et_lishi_setting_input_ip);
        pointName = findViewById(R.id.if_lishi_setting_point_name);
        locationName = findViewById(R.id.if_lishi_setting_location_name);
        meationName = findViewById(R.id.if_lishi_setting_meation_name);
        btBind = findViewById(R.id.bt_lishi_setting_binding);
        btUnBind = findViewById(R.id.bt_lishi_setting_unbinding);
        btQuit = findViewById(R.id.bt_quit_app);
        btBack = findViewById(R.id.bt_lishi_setting_back);

        //数据初始化，对控件赋值
        ipInput.setText((String) SPUtils.get(LiShiSettingActivity.this, Customer.LiSHI_INPUT_URL, ""));

        pointName.setEditName((String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_POINT_NAME, ""));
        locationName.setEditName((String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_LOCATION_NAME, ""));
        meationName.setEditName((String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_MEATION_NAME, ""));

        scenicSpotBusinessOrgId = (Long) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_POINT_ID, 0L);
        scenicSpotBusinessOrgName = (String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_POINT_NAME, "");

        locationId = (Long) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_LOCATION_ID, 0L);
        locationNa = (String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_LOCATION_NAME, "");

        terminalId = (Long) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_MEATION_ID, 0L);
        terminalName = (String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_MEATION_NAME, "");


        doThing();
        ShowPointList();
        ShowLocationList();
        ShowTerminalList();
    }

    private void doThing() {

        //绑定操作
        btBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //去绑定
                iosLoadDialog.show();
                if (StringUtil.isNullOrEmpty(terminalId + "")) {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast("未选择终端名称");
                    return;
                }
                addBean.setTerminalId(terminalId + "");
                addBean.setTerminalComputerCpuId((String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_DEVICE_ID, ""));
                addBean.setTerminalSetFlag(0 + "");
                addBean.setTerminalComputerName("手持机");
                setBindingMeation();

            }
        });

        //取消绑定操作
        btUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long MeationId = (Long) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_MEATION_ID, 0L);
                if (StringUtil.isNullOrEmpty(MeationId + "")) {
                    //如果为空
                    SelfToastUtil.getInstance().show("保存的绑定终端号为空，请确认");
                    return;
                } else {
                    addBean.setTerminalComputerCpuId((String) SPUtils.get(LiShiSettingActivity.this, Customer.LISHI_DEVICE_ID, ""));
                    addBean.setTerminalId(MeationId + "");
                    addBean.setTerminalSetFlag(1 + "");
                    addBean.setTerminalComputerName("手持机");
                    setUnBindingMeation();
                }

            }
        });

        btQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishAllAcitivity();

            }
        });


        pointName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iosLoadDialog.show();
                getPointList();
            }
        });

        locationName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scenicSpotBusinessOrgId == null) {
                    ToastUtil.showToast("请先选择景点名称");
                    return;
                }
                iosLoadDialog.show();
                getLocationList();
            }
        });

        meationName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (locationId == null) {
                    ToastUtil.showToast("请先选择点位名称");
                    return;
                }
                iosLoadDialog.show();
                getMeationList();
            }
        });

        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void setUnBindingMeation() {

        RetrofitClient.getInstance(LiShiSettingActivity.this, Base_URL, headers).JJHaddOrRelieveTerminalBind(addBean, new BaseSubscriber<JJHaddOrRelieveResultBean>(LiShiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHaddOrRelieveResultBean jjHaddOrRelieveResultBean) {
                if (jjHaddOrRelieveResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    //绑定成功 去保存信息
                    // 如果解绑成功，那么就要去 删除数据
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_POINT_ID, 0L);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_POINT_NAME, "");

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_LOCATION_ID, 0L);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_LOCATION_NAME, "");

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_MEATION_ID, 0L);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_MEATION_NAME, "");

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_GET_NO, "");

                    pointName.setEditName("");
                    locationName.setEditName("");
                    meationName.setEditName("");
//                    setResult(202);
//                    finish();
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHaddOrRelieveResultBean.getMessage());
                }
            }
        });
    }


    // 点击去绑定操作
    private void setBindingMeation() {
        RetrofitClient.getInstance(LiShiSettingActivity.this, Base_URL, headers).JJHaddOrRelieveTerminalBind(addBean, new BaseSubscriber<JJHaddOrRelieveResultBean>(LiShiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(JJHaddOrRelieveResultBean jjHaddOrRelieveResultBean) {
                if (jjHaddOrRelieveResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    //绑定成功 去保存信息
                    // 绑定成功后，需要保存数据
                    SPUtils.put(LiShiSettingActivity.this, Customer.LiSHI_INPUT_URL, ipInput.getText().toString().trim());

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_POINT_ID, scenicSpotBusinessOrgId);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_POINT_NAME, scenicSpotBusinessOrgName);

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_LOCATION_ID, locationId);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_LOCATION_NAME, locationNa);

                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_MEATION_ID, terminalId);
                    SPUtils.put(LiShiSettingActivity.this, Customer.LISHI_MEATION_NAME, terminalName);

                    SelfToastUtil.getInstance().show(jjHaddOrRelieveResultBean.getMessage());
                    terminalId = 0L;
//                    setResult(202);
//                    finish();
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHaddOrRelieveResultBean.getMessage());
                }
            }
        });
    }

    private void getPointList() {
        RetrofitClient.getInstance(LiShiSettingActivity.this, Base_URL, headers).JJHgetScenicSpotBusinessList(new BaseSubscriber<LiShiPointListResultBean>(LiShiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(LiShiPointListResultBean jjhPointListResultBean) {
                if (jjhPointListResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    pointListData.clear();
                    pointListString.clear();
                    for (int i = 0; i < jjhPointListResultBean.getData().size(); i++) {
                        LogUtil.d(jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgShortName());
                        pointListData.add(jjhPointListResultBean.getData().get(i));
                        pointListString.add(jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgShortName() + "  " + jjhPointListResultBean.getData().get(i).getScenicSpotBusinessOrgId());
                    }
                    if (pointListString.size() > 0) {
                        pvNoLinkOptions.show();
                    } else {
                        ToastUtil.showToast("没有可用景点");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjhPointListResultBean.getMessage());
                }
            }
        });
    }

    private void getLocationList() {
        findLocationBean.setScenicSpotBusinessOrgId(scenicSpotBusinessOrgId);
        RetrofitClient.getInstance(LiShiSettingActivity.this, Base_URL, headers).JJHfindPointSelect(findLocationBean, new BaseSubscriber<LiShifindLocationResultBean>(LiShiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(LiShifindLocationResultBean jjHfindPointResultBean) {
                if (jjHfindPointResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    locationListData.clear();
                    LocationListString.clear();
                    for (int i = 0; i < jjHfindPointResultBean.getData().size(); i++) {
                        LogUtil.d(jjHfindPointResultBean.getData().get(i).getPointShortName());
                        locationListData.add(jjHfindPointResultBean.getData().get(i));
                        LocationListString.add(jjHfindPointResultBean.getData().get(i).getPointShortName() + "  " + jjHfindPointResultBean.getData().get(i).getPointId());
                    }

                    if (LocationListString.size() > 0) {
                        pvNoLinkOptionsecond.show();
                    } else {
                        ToastUtil.showToast("没有可用点位");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHfindPointResultBean.getMessage());
                }
            }
        });
    }

    private void getMeationList() {
        meationBean.setTerminalType(5);
        meationBean.setPointId(locationId);
        meationBean.setTerminalResignFlag(0L);
        RetrofitClient.getInstance(LiShiSettingActivity.this, Base_URL, headers).JJHfindTerminalNameAndIdList(meationBean, new BaseSubscriber<LiShifindTerminalNameResultBean>(LiShiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.code + e.message);
                iosLoadDialog.dismiss();
            }

            @Override
            public void onNext(LiShifindTerminalNameResultBean jjHfindTerminalNameResultBean) {
                if (jjHfindTerminalNameResultBean.getCode().equals("200")) {
                    iosLoadDialog.dismiss();
                    meationList.clear();
                    meationListString.clear();
                    for (int i = 0; i < jjHfindTerminalNameResultBean.getData().size(); i++) {
                        LogUtil.d(jjHfindTerminalNameResultBean.getData().get(i).getTerminalShortName());
                        meationList.add(jjHfindTerminalNameResultBean.getData().get(i));
                        meationListString.add(jjHfindTerminalNameResultBean.getData().get(i).getTerminalShortName() + "  " + jjHfindTerminalNameResultBean.getData().get(i).getTerminalId());
                    }

                    if (meationListString.size() > 0) {
                        pvNoLinkOptionThread.show();
                    } else {
                        ToastUtil.showToast("没有可用点位");
                    }
                } else {
                    iosLoadDialog.dismiss();
                    ToastUtil.showToast(jjHfindTerminalNameResultBean.getMessage());
                }
            }
        });
    }

    private void ShowPointList() {
        pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                pointName.setEditName(pointListData.get(options1).getScenicSpotBusinessOrgShortName() + "");
                scenicSpotBusinessOrgId = pointListData.get(options1).getScenicSpotBusinessOrgId();
                scenicSpotBusinessOrgName = pointListData.get(options1).getScenicSpotBusinessOrgShortName();
            }
        })
                .setItemVisibleCount(4)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptions.setNPicker(pointListString, null, null);
        pvNoLinkOptions.setSelectOptions(0, 1, 1);

    }

    private void ShowLocationList() {

        pvNoLinkOptionsecond = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                locationName.setEditName(locationListData.get(options1).getPointShortName() + "");
                locationId = locationListData.get(options1).getPointId();
                locationNa = locationListData.get(options1).getPointShortName();
            }
        })
                .setItemVisibleCount(4)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptionsecond.setNPicker(LocationListString, null, null);
        pvNoLinkOptionsecond.setSelectOptions(0, 1, 1);

    }

    private void ShowTerminalList() {

        pvNoLinkOptionThread = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                meationName.setEditName(meationList.get(options1).getTerminalShortName() + "");
                terminalId = meationList.get(options1).getTerminalId();
                terminalName = meationList.get(options1).getTerminalShortName();
            }
        })
                .setItemVisibleCount(4)
//                 .setSelectOptions(0, 1, 1)
                .build();
        pvNoLinkOptionThread.setNPicker(meationListString, null, null);
        pvNoLinkOptionThread.setSelectOptions(0, 1, 1);

    }
}
